//
//  LWAddLocationVc.m
//  LWShopItem
//
//  Created by mac on 17/5/20.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWAddLocationVc.h"
#import "UIColor+Addition.h"
#import "LWAddressViewController.h"
#import "LWContactCell.h"

static NSString *cellId = @"cellId";

@interface LWAddLocationVc ()<LWDetilVcDelegate>

@end

@implementation LWAddLocationVc
{
    // 泛型
    NSMutableArray<LWAccountModel *>* _contactList;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //    [self loadData];
    [self.tableView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadData];
    
}

#pragma MARK - UI
- (void)setupUI
{
    self.title = @"管理收货地址";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:0x9ACD32];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backCLick)];
    
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    UIBarButtonItem *rigtItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rigthItemCilck)];
    
    self.navigationItem.rightBarButtonItem = rigtItem;
    
    self.navigationItem.rightBarButtonItems = @[self.navigationItem.rightBarButtonItem,self.editButtonItem];
    
    self.tableView.rowHeight = 80;
    
    //注册
    [self.tableView registerClass:[LWContactCell class] forCellReuseIdentifier:cellId];
    
}

#pragma MARK - 数据存储的方法
- (void)saveData
{
    //获取路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).lastObject  stringByAppendingString:@"LWperson.db"];
    
    
    //归档
    [NSKeyedArchiver archiveRootObject:_contactList toFile:filePath];
    
}

#pragma MARK - Data
- (void)loadData
{
    //从文件读取
    //获取路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).lastObject  stringByAppendingString:@"LWperson.db"];
    

    //读取数据
    _contactList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    //如果没有值 实例化_contactList
    if (_contactList == nil) {
        
        _contactList = [NSMutableArray array];
        
    }
    
}
#pragma MARK - 添加按钮的点击方法
- (void)rigthItemCilck
{
    LWAddressViewController *vc = [LWAddressViewController new];
    
    vc.delegate = self;
    
    self.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma MARK - 添加完成的代理方法
- (void)detilVc:(LWAddressViewController *)detilVc addModel:(LWAccountModel *)newModel
{
    //保存模型数据
    [_contactList insertObject:newModel atIndex:0];
    
    //刷新
    [self.tableView reloadData];
    
    //保存
    [self saveData];
}
#pragma MARK - 编辑完成的代理方法
- (void)detilVcDidFinishEndingContact:(LWAddressViewController *)detilVc
{
    //刷新数据
    [self.tableView reloadData];
    
    //保存
    [self saveData];
}

#pragma MARK - 点击跳转的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LWAccountModel *selModel = _contactList[indexPath.row];
    [_contactList removeObject:selModel];
    [_contactList insertObject:selModel atIndex:0];
    LWAddressViewController *vc = [LWAddressViewController new];
    
    
    //设置代理
    vc.delegate = self;
    
    vc.model = selModel;
    
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
//侧滑删除效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //获取模型数据
        LWAccountModel *delModel = _contactList[indexPath.row];
        
        //删除
        [_contactList removeObject:delModel];
        //保存
        [self saveData];
        
        //刷新
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        
        
        
        
    }
    
}



#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _contactList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LWContactCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    cell.model = _contactList[indexPath.row];
    
    return cell;
}


#pragma MARK - btnClick
- (void)backCLick
{
    [self saveData];
    [self.navigationController popViewControllerAnimated:YES];
    
}



@end
